Fastify is a small Node framework for developing back end web apps.
In this article, we’ll look at how to create back end apps with Fastify.
Set Error Handler
We can call setErrorHandler
to ad an error handler.
For example, we can write:
const fastify = require('fastify')({
logger: true
})
fastify.setErrorHandler(function (error, request, reply) {
this.log.error(error)
reply.status(409).send({ ok: false })
})
fastify.get('/', async (request, reply) => {
return 'hello world'
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
await fastify.close()
process.exit(1)
}
}
start()
to call setErrorHandler
with our handler.
We can get the status code with the error.statusCode
property:
const fastify = require('fastify')({
logger: true
})
fastify.setErrorHandler(function (error, request, reply) {
const { statusCode } = error.statusCode
if (statusCode >= 500) {
this.log.error(error)
} else if (statusCode >= 400) {
this.log.info(error)
} else {
this.log.error(error)
}
reply.status(409).send({ ok: false })
})
fastify.get('/', async (request, reply) => {
return 'hello world'
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
await fastify.close()
process.exit(1)
}
}
start()
We check the statusCode
in our error handler.
Print a List of Routes
We can print the list of routes with the fastify.printRoutes
method.
For instance, we can write:
const fastify = require('fastify')({
logger: true
})
fastify.ready(() => {
console.log(fastify.printRoutes())
})
fastify.get('/', async (request, reply) => {
return 'hello world'
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
to print the listen of routes in the fastify.ready
handler.
We get:
└── / (GET)
because we defined the /
route with get
.
Content-Type Parser
We can add our own content type parser with the fastify.addContentTypeParser
method.
For example, we can write:
const fastify = require('fastify')({
logger: true
})
fastify.addContentTypeParser('text/json', { asString: true }, fastify.getDefaultJsonParser('ignore', 'ignore'))
fastify.get('/', async (request, reply) => {
return 'hello world'
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
We add a content type handler for the text/json
MIME-type.
Error Handler
We can add an error handler with the fastify.errorHandler
method.
For example, we can write:
const fastify = require('fastify')({
logger: true
})
fastify.get('/', {
errorHandler: (error, request, reply) => {
if (error.code === 'SOMETHING_SPECIFIC') {
reply.send({ custom: 'response' })
return
}
fastify.errorHandler(error, request, response)
}
}, (request, reply) => {
return 'hello world'
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
to add our /
route with an error handler.
We check the error.code
property to check for the type of error raised.
Then we call reply.send
to return the response when an error occurred.
Conclusion
We can set an error handler, content type parser, and print a list of routes with Fastify.